package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ChanPinFenLeiDaoImpl;
import com.ysd.entity.ChanPinFenLei;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

/**
 * Servlet implementation class AdminProductCategoryServlet
 */
public class AdminProductCategoryServlet extends BaseServlet {
	/**
	 *获取产品分类
	 *localhost：8080/shangcheng/adminProductCategory?reqName=getCpFenLei
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getCpFenLei(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ChanPinFenLeiDaoImpl cpDao=new ChanPinFenLeiDaoImpl();
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<ChanPinFenLei> cpList = cpDao.getChanPinFenLeiListByPage(page, size);//查询到的所有产品分类
		
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式
		PrintWriter out =response.getWriter();//out对象
		
		Gson gson = new Gson();
		
		out.print(gson.toJson(cpList));//响应数据  将cpList转化为json格式字符串
		
		out.flush();
		out.close();
	}
	
	/**
	 *删除产品分类
	 *localhost：8080/shangcheng/adminProductCategory?reqName=delChanPinFL
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delChanPinFL(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int cpFLId=Integer.parseInt(request.getParameter("cpFenLeiId"));//接收前台传来的参数
		
		
		request.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out=response.getWriter();//out对象
		
		
		ChanPinFenLeiDaoImpl cpDao=new ChanPinFenLeiDaoImpl();//创建实现类对象
		int n=cpDao.delChanPinFLId(cpFLId);//调用实现类的删除方法，返回受影响行数
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		out.flush();
		out.close();
	}
	/**
	 * 批量删除
	 * localhost：8080/shangcheng/adminProductCategory?reqName=delChanPinFLIds
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
public void delChanPinFLIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String cpFLIds = request.getParameter("cpFLIds");//接收前台传来的参数
		
		
		request.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out=response.getWriter();//out对象
		
		
		ChanPinFenLeiDaoImpl cpDao=new ChanPinFenLeiDaoImpl();//创建实现类对象
		int n=cpDao.delChanPinFLIds(cpFLIds);//调用实现类的删除方法，返回受影响行数
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		out.flush();
		out.close();
	}

	
	/**
	 *添加产品分类
	 *localhost：8080/shangcheng/adminProductCategory?reqName=addcpFenLei
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addcpFenLei(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out=response.getWriter();//out对象
		ChanPinFenLeiDaoImpl cpDao=new ChanPinFenLeiDaoImpl();//调用实现类对象
		
		String cPName=request.getParameter("cpName");//接收前台传来的参数
		if(cPName!=null && !cPName.equals("")) {
			List<ChanPinFenLei> cpList=cpDao.getChanPinFenLei(cPName);
			if(cpList.size()==0) {//==0没有查到
				ChanPinFenLei cpM=new ChanPinFenLei(0, cPName);//调用有参的构造方法创建产品对象
				System.out.println(cpM);//输出看产品对象里面有没有东西
				
				
				int n=cpDao.addCPFenLei(cpM);//n 受影响行数
				if(n>0) {
					out.print(Result.toClient(true,"添加成功",""));
				}else {
					out.print(Result.toClient(false,"添加失败",""));
				}
			}else {//查到了不能添加
				out.print(Result.toClient(false,"分类名已存在",""));
			}
		}
		
		
		out.flush();
		out.close();
	}
	
	/**
	 *编辑产品信息
	 *localhost：8080/shangcheng/adminProductCategory?reqName=getcpId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getcpId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		ChanPinFenLeiDaoImpl cpDao=new ChanPinFenLeiDaoImpl();
		int cpId=Integer.parseInt(request.getParameter("cpId"));
		ChanPinFenLei cp= cpDao.getcPId(cpId);
		
		Gson gson=new Gson();
		String res=gson.toJson(cp);
		out.print(res);
		
		out.flush();
		out.close();
	}
	/**
	 * localhost：8080/shangcheng/adminProductCategory?reqName=updateChanPin
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateChanPin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		ChanPinFenLeiDaoImpl cpDao=new ChanPinFenLeiDaoImpl();
		int cPFenLeiId=Integer.parseInt(request.getParameter("cpid"));
		String cPName=request.getParameter("cpname");
		if(cPName!=null && !cPName.equals("")) {
			List<ChanPinFenLei> cpList=cpDao.getChanPinFenLei(cPName);
			if(cpList.size()==0) {
				ChanPinFenLei fl=new ChanPinFenLei(cPFenLeiId, cPName);
				int n=cpDao.updateChanPinFenLei(fl);
				if(n>0) {
					out.print(Result.toClient(true, "修改成功",""));
				}else {
					out.print(Result.toClient(false, "修改失败",""));
				}
			}
			
		}else {
			out.print(Result.toClient(false, "分类名已存在",""));
		}
		
		
		out.flush();
		out.close();
	}
}
